import django_filters
from django.shortcuts import render
from rest_framework import viewsets,mixins
from rest_framework.decorators import action
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.pagination import PageNumberPagination
from rest_framework import filters
from django_filters.rest_framework import DjangoFilterBackend,FilterSet
from goods import models
from goods import serializers
from rest_framework.filters import OrderingFilter

# Create your views here.

class goodspag(PageNumberPagination):
  page_size = 2
  page_size_query_param = 'page_size'

class Goodsfil(FilterSet):
    goodnames = django_filters.CharFilter(field_name='goodname',lookup_expr='contains')
    prices = django_filters.NumberFilter(field_name='price',lookup_expr='gte')
    numbers = django_filters.NumericRangeFilter(field_name='number',lookup_expr='range')
    br = django_filters.CharFilter(field_name='brand__name')
    class Meta:
        model = models.Goods
        fields = ['goodnames','prices','numbers','br']


class BrandView(viewsets.ModelViewSet):
    queryset = models.Brand.objects.all()
    serializer_class = serializers.BrandSer

class GoodsView(viewsets.ModelViewSet):
    queryset = models.Goods.objects.all()
    serializer_class = serializers.GoodsSer
    pagination_class = goodspag

    filter_backends = [DjangoFilterBackend,OrderingFilter]
    filterset_class = Goodsfil

    ordering_fields = ['price']
    ordering = ['price']

    @action(methods=['get'],detail=False)
    def hot(self,request):
        return Response({'mess': '新闻'})

    @action(methods=['get'], detail=True)
    def hots(self, request,pk):
        goods = self.get_object()
        return Response({'mess': '新闻11'})

    @action(methods=['get'], detail=True,url_path='a')
    def hot(self, request, pk):
        goods = self.get_object()
        return Response({'mess': '新闻2'})

# class BrandView(GenericAPIView):
#     queryset = models.Brand.objects.all()
#     serializer_class = serializers.BrandSer
#     def get(self,request):
#         ser = self.get_serializer(self.get_queryset(),many=True)
#         return Response(ser.data)
#     def post(self,request):
#         ser = self.get_serializer(data=request.data)
#         if ser.is_valid():
#             ser.save()
#             return Response({'msg':ser.data})
#         else:
#             return Response({"msg":"格式不对","data":ser.errors})
#
# class BrandDetail(GenericAPIView):
#     queryset = models.Brand.objects.all()
#     serializer_class = serializers.BrandSer
#     def get(self,request,pk):
#         ser = self.get_serializer(self.get_object())
#         return Response({"msg":"ok","data":ser.data})
#     def put(self,request,pk):
#         ser = self.get_serializer(self.get_object(),data=request.data)
#         if ser.is_valid():
#             ser.save()
#             return Response({'msg':ser.data,'abc':'修改成功'})
#         else:
#             return Response({"msg":"格式不对","data":ser.errors})
#
#     def delete(self, request, pk):
#         self.get_object().delete()
#         return Response({'message':'删除成功','code':400})
#
# class GoodsView(GenericAPIView):
#     queryset = models.Goods.objects.all()
#     serializer_class = serializers.GoodsSer
#     def get(self,request):
#         ser = self.get_serializer(self.get_queryset(),many=True)
#         return Response(ser.data)
#     def post(self,request):
#         ser = self.get_serializer(data=request.data)
#         if ser.is_valid():
#             ser.save()
#             return Response({'msg':ser.data})
#         else:
#             return Response({"msg":"格式不对","data":ser.errors})
#
# class GoodsDetail(GenericAPIView):
#     queryset = models.Goods.objects.all()
#     serializer_class = serializers.GoodsSer
#     def get(self,request,pk):
#         ser = self.get_serializer(self.get_object())
#         return Response({"msg":"ok","data":ser.data})
#     def put(self,request,pk):
#         ser = self.get_serializer(self.get_object(),data=request.data)
#         if ser.is_valid():
#             ser.save()
#             return Response({'msg':ser.data,'abc':'修改成功'})
#         else:
#             return Response({"msg":"格式不对","data":ser.errors})
#
#     def delete(self, request, pk):
#         self.get_object().delete()
#         return Response({'message':'删除成功','code':400})





